#include <crt_dl.h>

HMODULE loadlib(basic_tchar* path, int flag) {

#if (TARGET_OS == OS_WINDOWS)
	return LoadLibrary(path);
#elif (TARGET_OS == OS_POSIX)
	return dlopen(path, flag);
#elif (TARGET_OS == OS_DARWIN)
    return dlopen(path, flag);
#endif
    return 0;
}

void* libfunc(HMODULE handler, char* sym) {

#if (TARGET_OS == OS_WINDOWS)
	return GetProcAddress(handler, sym);
#elif (TARGET_OS == OS_POSIX)
	void* pFunc = dlsym(handler, sym);
	if (pFunc == NULL) {
		char* pszErr = dlerror();
		printf("%s\n", pszErr);
		return pFunc;
	}
	return pFunc;
#elif (TARGET_OS == OS_DARWIN)
    void* pFunc = dlsym(handler, sym);
    if (pFunc == NULL) {
        char* pszErr = dlerror();
        printf("%s\n", pszErr);
        return pFunc;
    }
    return pFunc;
#endif
	return 0;
}

int closelib(HMODULE handler) {
#if (TARGET_OS == OS_WINDOWS)
	return FreeLibrary(handler);
#elif (TARGET_OS == OS_POSIX)
	return dlclose(handler);
#elif (TARGET_OS == OS_DARWIN)
    return dlclose(handler);
#endif
	return 0;
}
